import { Module, BadRequestException } from '@nestjs/common';
import { ProductController } from './product.controller';
import { ProductService } from './product.service';
import { Product } from './product.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RedisModule } from '../redis/redis.module';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { v4 as uuidv4 } from 'uuid';

const image = ['png', 'jpg', 'jpeg', 'bmp', 'webp'];
@Module({
  imports: [TypeOrmModule.forFeature([Product]), RedisModule, MulterModule.register({
    storage: diskStorage({
      // 配置文件上传后的文件夹路径
      destination: (req, file, cb) => {
        const filePath = 'static/product/upload';
        return cb(null, `./${filePath}`);
      },
      filename: (req, file, cb) => {
        // 在此处自定义保存后的文件名称
        const fileType = file.originalname.split('.')[1];
        const fileName = uuidv4() + "." + fileType;
        return cb(null, fileName);
      },
    }),
    fileFilter(req, file, cb) {
      return cb(null, true);
    },
  })
  ],
  controllers: [ProductController],
  providers: [ProductService],
})
export class ProductModule { }
